﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Models;
using App.Models.Pager;
using App.Views.ViewModels;
namespace App.Controllers
{
    public class FeeQueryServiceController :  BaseController
    {

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult List(int? page, int? rows, string order)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;

            //int offset = (page ?? 1 - 1) * (rows ?? 10);
            List<vi_ServiceDetail> list = null;
            //默认选择最后一个死者
            var dead = dbe.tb_RegistrationInfo.OrderByDescending(m => m.DeadNO).FirstOrDefault();
            if (dead == null)
            {
                return Json(new List<vi_ServiceDetail>(), JsonRequestBehavior.AllowGet);
            }
            if ("asc" == order)
            {
                list = dbe.vi_ServiceDetail.Where(m => m.DeadNO == dead.DeadNO).OrderBy(m => m.DeadNO).ToList();
            }
            else
            {
                list = dbe.vi_ServiceDetail.OrderByDescending(m => m.DeadNO).ToList();
            }
            decimal? sum = list.Sum(m => m.ServiceFee);
            var foot = new vi_ServiceDetail
            {
                ServiceFee = sum,
                DeadNO = "合计："
            };
            return Json(new
            {
                success = true,
                rows = list,
                footer = new object[] { foot }
            }, JsonRequestBehavior.AllowGet);

        }

        //根据死者编号查询
        public ActionResult Search(FormCollection collection)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;

            string starttime=Request.Form["StartTime"];
            string endtime=Request.Form["EndTime"];
            string type =Request.QueryString["type"];

            DateTime st = DateTime.MinValue;
            DateTime et = DateTime.MaxValue;
            try
            {
                st = DateTime.Parse(starttime);
            }
            catch
            {
                st = DateTime.MinValue;
            }
            try
            {
                et = DateTime.Parse(endtime);
                et = et.AddDays(1);
            }
            catch
            {
                et = DateTime.MaxValue;
            }

            //string deadName = collection["DeadName"];

            var list = dbe.vi_ServiceDetail.Where(m => ((m.PayTime == null || (st <= m.PayTime) && et >= m.PayTime)));
            if (type != "0000") {
                list = list.Where(m => m.ParentServiceID == type);
            }
            list = list.OrderBy(m => m.DeadNO);
            decimal? sum=list.Sum(m=>m.ServiceFee);
            var foot = new vi_ServiceDetail
            {
                ServiceFee = sum,
                DeadNO = "合计："
            };
            return Json(new
            {
                success = true,
                rows = list,
                footer = new object[] { foot }
            }, JsonRequestBehavior.AllowGet);
        }
        //统计各项收入情况
        public ActionResult IncomeIndex()
        {
            return View();
        }        
    }
}
